﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AndroidEditor.WForm;
using System.Xml;
namespace AndroidEditor
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    class GlobleSetting
    {
        /// <summary>
        /// Gets the current dir.
        /// </summary>
        /// <remarks></remarks>
        public static string CurrentDir { get { return Application.StartupPath; } }
        /// <summary>
        /// 
        /// </summary>
        private static GlobleSetting sSetting = null;
        /// <summary>
        /// 
        /// </summary>
        private string path = "";
        /// <summary>
        /// 
        /// </summary>
        private string CompilerPath = "";
        /// <summary>
        /// 
        /// </summary>
        private XmlDocument loadxml = null;
        /// <summary>
        /// 
        /// </summary>
        private XmlNode root = null;
        /// <summary>
        /// Prevents a default instance of the <see cref="GlobleSetting"/> class from being created.
        /// </summary>
        /// <remarks></remarks>
        private GlobleSetting()
        {
            LoadXml();
        }

        /// <summary>
        /// Loads the XML.
        /// </summary>
        /// <remarks></remarks>
        private void LoadXml()
        {
             loadxml = new XmlDocument();
             if (System.IO.File.Exists(GlobleSetting.CurrentDir + @"\Config.xml"))
             {
                 loadxml.Load(GlobleSetting.CurrentDir + @"\Config.xml");
                 root = loadxml.GetElementsByTagName("Root")[0];

                 XmlNode  ndk = FindOrCreateNew(root, "NDK");
                 path = FindOrCreateNewAttribute(ndk, "value").Value;
                 CompilerPath = FindOrCreateNewAttribute(ndk, "Compiler").Value;
                  
             }
             else
             {
                 
               root  =   loadxml.CreateNode(XmlNodeType.Element, "Root","");
               loadxml.AppendChild(root);
                
             }
            

        }
        /// <summary>
        /// Instances this instance.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public static GlobleSetting Instance()
        {
            if (sSetting == null)
            {
                sSetting = new GlobleSetting();
            }
            return sSetting;
        }




        /// <summary>
        /// Sets the N dk path.
        /// </summary>
        /// <param name="ndkpath">The ndkpath.</param>
        /// <remarks></remarks>
        public void SetNDkPath(string  ndkpath)
        {
            path = ndkpath;
            XmlNode  NdkNode = FindOrCreateNew(root, "NDK");
            XmlAttribute value = FindOrCreateNewAttribute(NdkNode, "value");
            value.Value = path;
        }

        /// <summary>
        /// Finds the or create new attribute.
        /// </summary>
        /// <param name="NdkNode">The NDK node.</param>
        /// <param name="Name">The name.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private XmlAttribute FindOrCreateNewAttribute(XmlNode NdkNode, string Name)
        {

            foreach (XmlAttribute att in NdkNode.Attributes)
            {
                if (att.Name == Name)
                {
                    return att;
                }
            }
            XmlAttribute value = NdkNode.OwnerDocument.CreateAttribute(Name);
            NdkNode.Attributes.Append(value);
            return value;
        }

        /// <summary>
        /// Finds the or create new.
        /// </summary>
        /// <param name="root">The root.</param>
        /// <param name="NodeName">Name of the node.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private XmlNode FindOrCreateNew(XmlNode root, string  NodeName)
        {
            foreach (XmlNode node in root.ChildNodes)
            {
                if (node.Name == NodeName)
                {
                    return (XmlElement)node;
                }
            }
            XmlNode  nNode =  loadxml.CreateNode(XmlNodeType.Element, NodeName,"");
            root.AppendChild(nNode);
            return nNode;

        }

        /// <summary>
        /// Gets the NDK.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public string GetNDK()
        {
            return path;
        }

        /// <summary>
        /// Saves the specified file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <remarks></remarks>
        public void Save(string  file)
        {
            loadxml.Save(file);
        }

        /// <summary>
        /// Gets the compiler DIR.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public string GetCompilerDIR()
        {
            return CompilerPath;
        }

        /// <summary>
        /// Sets the compiler path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <remarks></remarks>
        public void SetCompilerPath(string path)
        {
            CompilerPath = path;
            XmlNode NdkNode = FindOrCreateNew(root, "NDK");
            XmlAttribute value = FindOrCreateNewAttribute(NdkNode, "Compiler");
            value.Value = path;
        }
    }
}
